<?php
// +----------------------------------------------------------------------
// | yiran [ 张依然 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 张依然 <439583514@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 邮箱验证码接口
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\common\controller\Base;
use app\common\model\Ems as Ems_Model;
use app\member\model\Member;
use Firebase\JWT\JWT;
use think\facade\Validate;

class Account extends Base{
    public function _initialize(){
        $this -> checkToken();
    }
    //用于登陆
    public function login(){
        $data = input('get.');
       // $store_model = new StoreModel;
        $data["token"] = $this -> jwtEncode();//生成token
        //$login_data = $store_model -> judgeLogin($data);
        return json($data);//返回登陆信息及token
    }

    //用于生成token数据并返回
    public function jwtEncode(){
        $type = config('jwt_type');
        $key = config('jwt_key');

        $token = array(
            "iss" => "hahaha",//该JWT的签发者，用于后面的解密操作
            "iat" => time(),//创建时间
            "exp" => time()+7200//什么时候过期
        );

        $jwt = JWT::encode($token, $key);
        return $jwt;
    }

//解析token
    public function checkToken(){
        $request = Request::instance();
        $jwtToken = $request -> header('Authorization');//获取小程序携带过来的签名token
        if(!$jwtToken){
            die("444");
        }
        $key = config('jwt_key');//解密秘钥
        $type = config('jwt_type');//签名类型HS256
        // JWT::$leeway = 10;//偏移时间
        try{
            $json = JWT::decode($jwtToken,$key,array($type));//解密签名token
            return $json;
        }catch(\Exception $exception){//如果解密失败，或者超过有效期则die
            die("444");
        }
    }

}